package com.crazy.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author panyonghui
 * @describe 链接请求工具类
 * @date 2022/2/16 15:14
 */
public class HttpUtils {
    /**
     * @describe 发送请求
     * @auther crazy_cat
     * @date 2022/2/16 15:34
     * @param url
     * @param paramMap
     * @return com.alibaba.fastjson.JSONObject
     * @modify
     */
    public static JSONObject requestByRestTemplate(String url, Map paramMap){
        // 设置超时时间
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(1000);
        requestFactory.setReadTimeout(1000);

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        // 设置请求体
        HttpEntity httpEntity = new HttpEntity(paramMap);

        //利用复杂构造器可以实现超时设置，内部实际实现为 HttpClient
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        // 发送请求
        ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, JSONObject.class);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            // 请求失败
        }
        return responseEntity.getBody();
    }

    public static JSONObject sendFile(String url, File file, String fileName){
        // 设置超时时间
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(1000);
        requestFactory.setReadTimeout(1000);

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("version", "1.0");

        //设置content-type
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);

        //设置请求体
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("file", fileSystemResource);
        form.add("fileName", fileName);

        //用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);

        RestTemplate restTemplate = new RestTemplate(requestFactory);
        //服务端返回的json格式："
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForObject(url, files, ResponseEntity.class);
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            // 请求失败
        }
        return responseEntity.getBody();
    }
}
